#!/bin/bash
# 
# Copyright 2019 Mason Hock <mason@masonhock.com>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# determine browser
if [[ $(basename $1) == "icecat"*"gnulinux"*"tar.bz2" ]]; then
	BROWSER=icecat	
	echo "installing Icecat"
elif [[ $(basename $1) == "tor-browser-linux"*"tar.xz" ]]; then
	BROWSER=tor-browser
	echo "installing Tor Browser"
else
	echo "invalid tarball. see
	https://www.gnu.org/software/gnuzilla/
	https://www.torproject.org/download/download.html"
	exit 0
fi

# create directories
[[ -d ~/.local ]] || mkdir ~/.local
[[ -d ~/.local/bin ]] || mkdir ~/.local/bin
[[ -d ~/.local/lib ]] || mkdir ~/.local/lib
[[ -d ~/.local/share ]] || mkdir ~/.local/share
[[ -d ~/.local/share/applications ]] || mkdir ~/.local/share/applications

# add ~/.local/bin to PATH
if ! echo $PATH | grep $HOME/.local/bin; then
	echo 'PATH="$HOME/.local/bin:$PATH"' >> $HOME/.profile
fi

# install
mkdir ~/.local/lib/$BROWSER
tar xf $1 --directory ~/.local/lib/$BROWSER --strip-components=1
if [[ $BROWSER = icecat ]]; then
	ln -s ~/.local/lib/icecat/icecat ~/.local/bin/icecat
	cp icecat.desktop ~/.local/share/applications/icecat.desktop
	sed -i "s|Icon=icecat|Icon=$HOME/.local/lib/icecat/browser/chrome/icons/default/default128.png|" ~/.local/share/applications/icecat.desktop
elif [[ $BROWSER = tor-browser ]]; then
	ln -s ~/.local/lib/tor-browser/Browser/start-tor-browser ~/.local/bin/tor-browser
	ln -s ~/.local/lib/tor-browser/start-tor-browser.desktop ~/.local/share/applications/tor-browser.desktop
fi
